﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Web.Services;
using System.Web.Script.Services;
using Newtonsoft.Json;
using System.IO;
using System.Web.UI.HtmlControls;

namespace PromotionEngine
{

    public partial class DocHeader : System.Web.UI.Page
    {
        public class Response
        {
            public List<Model.DocHeader> Data { get; set; }
            public int Count { get; set; }
            public string Errors { get; set; }

            public Response(List<Model.DocHeader> data, int count)
            {
                this.Data = data;
                this.Count = count;
            }

            public Response(string errors)
            {
                this.Errors = errors;
            }

            public Response()
            {

            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public static Response GetDocHeaders()
        {
            List<Model.DocHeader> lstCompany = new List<Model.DocHeader>();
            Promotion pmt = new Promotion();
            DataSet dsPromotion = pmt.GetAllDocHeader(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), HttpContext.Current.Session["CompanyCode"].ToString());
            if (dsPromotion != null)
            {

                foreach (DataRow r in dsPromotion.Tables[0].Rows)
                {
                    Model.DocHeader docheader = new Model.DocHeader();
                    docheader.CompanyCode = r["CompanyCode"].ToString();
                    docheader.CompanyName = r["CompanyName"].ToString();
                    docheader.CustomerCode = r["CustomerCode"].ToString();
                    docheader.CustomerID = r["CustomerID"].ToString();
                    docheader.CustomerName = r["CustomerName"].ToString();
                    docheader.DocDate = DateTime.Parse(r["DocDate"].ToString());
                    docheader.DocTime = DateTime.Parse(r["DocTime"].ToString());
                    docheader.DocDueDate = DateTime.Parse(r["DocDueDate"].ToString());
                    docheader.DocEntry = r["DocEntry"].ToString();
                    docheader.Remark = r["Remark"].ToString();
                    if (r["DocStatus"].ToString().Length > 0)
                    {
                        docheader.DocStatus = int.Parse(r["DocStatus"].ToString());
                    }
                    else
                    {
                        docheader.DocStatus = 1;
                    }
                    if (r["ReferenceName"].ToString().Length > 0)
                    {
                        docheader.ReferenceName = r["ReferenceName"].ToString();
                    }
                    else
                    {
                        docheader.ReferenceName = "Draft";
                    }
                    docheader.SubTotal = double.Parse(r["SubTotal"].ToString());
                    lstCompany.Add(docheader);
                }
                var jsonData = new { total = dsPromotion.Tables[0].Rows.Count, lstCompany };
                return new Response(lstCompany, dsPromotion.Tables[0].Rows.Count);
            }
            return new Response();
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public static string GetDocHeadersForSearch(string keySearch, string keyWords)
        {
            Promotion pmt = new Promotion();
            if (keySearch.Trim().Length == 0 && keyWords.Trim().Length == 0)
            {
                long count = pmt.GetCountItem(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), HttpContext.Current.Session["CompanyCode"].ToString());
                long maximunDisplay;
                long.TryParse(System.Configuration.ConfigurationManager.AppSettings["MaximunDisplayItems"].ToString(), out maximunDisplay);
                if (count <= maximunDisplay)
                {
                    DataSet dsPromotion = pmt.GetAllDocHeader(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), HttpContext.Current.Session["CompanyCode"].ToString());
                    if (dsPromotion != null && dsPromotion.Tables.Count > 0)
                    {
                        return JsonConvert.SerializeObject(dsPromotion.Tables[0]);
                    }
                }
                else
                {
                    return string.Empty;
                }
            }
            else
            {
                if (keySearch.Trim().Length > 0)
                {
                    DataSet dsPromotion = pmt.GetDocHeaderByDocEntry(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), HttpContext.Current.Session["CompanyCode"].ToString(), keySearch);
                    if (dsPromotion != null)
                    {
                        return JsonConvert.SerializeObject(dsPromotion.Tables[0]);
                    }
                }
                else if (keyWords.Trim().Length > 0)
                {
                    DataSet dsPromotion = pmt.GetDocHeaderByCustomerForSearch(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), HttpContext.Current.Session["CompanyCode"].ToString(),
                 keyWords);
                    if (dsPromotion != null)
                    {
                        return JsonConvert.SerializeObject(dsPromotion.Tables[0]);
                    }
                }
            }
            return string.Empty;
        }
        [WebMethod]
        public static void UpdateDoc(string oDocHeader, string oDocLine, bool isInsert)
        {
            Model.Company company = JsonConvert.DeserializeObject<Model.Company>(oDocHeader);
            Promotion pmt = new Promotion();
            string errMsg = pmt.UpdateJsonCompany(company.CompanyCode, company.CompanyName, company.IsActive, company.Address, company.ContactPerson, company.ContactPhone,
                  ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), isInsert);
            if (errMsg.Length == 0)
            {
                //Show error message
            }
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public static void DeleteDocument(string docEntry)
        {
            Promotion pmt = new Promotion();
            string errMsg = pmt.DeleteDocument(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), docEntry);
            if (errMsg.Length == 0)
            {
                //Show error message
            }
        }
    }
}